package com.boot.config;


import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Chunsheng.Zhang
 * @date 2021/1/12 16:40
 * @description : TODO
 */
@Configuration
public class CASAutoConfig {


    @Value("${cas.server-url-prefix}")
    private String serverUrlPrefix;

    @Value("${cas.server-login-url}")
    private String serverLoginUrl;

    @Value("${cas.client-host-url}")
    private String clientHostUrl;


    /**
     * @description: 授权过滤器
     * @author: ChunSheng.Zhang
     * @date: 2021/1/12 16:42
     * @return FilterRegistrationBean
     **/
    @Bean
    public FilterRegistrationBean filterAuthenticationRegistration() {

        //过滤器注册器
        FilterRegistrationBean registration = new FilterRegistrationBean();

        //设定使用什么过滤器
        registration.setFilter(new AuthenticationFilter());

        // 设定匹配的路径
        registration.addUrlPatterns("/*");
        Map<String,String> initParameters = new HashMap<>();
        initParameters.put("casServerLoginUrl", serverUrlPrefix);
        initParameters.put("serverName", clientHostUrl);
        //忽略的url，"|"分隔多个url
        initParameters.put("ignorePattern", "/logout/success|/index");
        registration.setInitParameters(initParameters);

        // 设定加载的顺序
        registration.setOrder(1);
        return registration;
    }




}
